﻿using RpcCentral.Model;
using RpcCentral.Model.DB;
using RpcHelper;
using RpcModel;
using SqlSugarHelper;

namespace RpcCentral.DAL.Repository
{
    internal class RemoteServerDAL : IRemoteServerDAL
    {
        private IRepository<RemoteServerConfig> _Db;
        public RemoteServerDAL(IRepository<RemoteServerConfig> db)
        {
            _Db = db;
        }
        public long Add(RemoteServerConfig add)
        {
          return _Db.InsertReutrnIdentity(add);
        }
        public RemoteServerModel GetRemoteServer(long id)
        {
            return this._Db.Get(a => a.Id == id, a => new RemoteServerModel
            {
                ApiVer = a.ApiVer,
                BindIndex = a.BindIndex,
                ConfigPrower = a.ConfigPrower,
                ConIp = a.ConIp,
                GroupId = a.GroupId,
                IsEnablePipe = a.IsEnablePipe,
                PublicKey = a.PublicKey,
                RecoveryLimit = a.RecoveryLimit,
                RecoveryTime = a.RecoveryTime,
                RegionId = a.RegionId,
                RemoteIp = a.RemoteIp,
                ServerIp = a.ServerIp,
                ServerMac = a.ServerMac,
                ServerName = a.ServerName,
                ServerPort = a.ServerPort,
                ServiceState = a.ServiceState,
                SystemType = a.SystemType
            });
        }
        public BasicServer[] GetRemoteServerConfig(long[] ids)
        {
            return this._Db.Gets(a => ids.Contains(a.Id) && a.ServiceState == RpcServiceState.正常, c => new BasicServer
            {
                Id = c.Id,
                LastOffliceDate = c.LastOffliceDate,
                IsOnline = c.IsOnline,
                RegionId = c.RegionId,
                VerNum = c.VerNum,
                Weight = c.Weight
            });
        }
        public long[] LoadServer(int serverIndex)
        {
            return this._Db.Gets(c => c.BindIndex == serverIndex && c.IsOnline, c => c.Id);
        }
        public void SetServerApiVer(long serverId, string ver)
        {
            if(!this._Db.Update(new RemoteServerConfig
            {
                Id = serverId,
                ApiVer = ver
            }, new string[] {
                "ApiVer"
            }))
            {
                throw new ErrorException("rpc.server.apiver.set.fail");
            }
        }
        public bool ServerOnline(long serverId, int serverIndex)
        {
            return this._Db.Update(a => new RemoteServerConfig
            {
                IsOnline = true,
                BindIndex = serverIndex
            }, a => a.Id == serverId && a.IsOnline == false);
        }
        public void SetConIp(long serverId, string conIp)
        {
            if(!this._Db.Update(new RemoteServerConfig
            {
                Id = serverId,
                ConIp = conIp
            }, new string[] {
                "ConIp"
            }))
            {
                throw new ErrorException("rpc.server.conIp.set.fail");
            }
        }
        public bool ServerOffline(long serverId, int serverIndex)
        {
            return this._Db.Update(a => new RemoteServerConfig
            {
                IsOnline = false,
                LastOffliceDate = HeartbeatTimeHelper.CurrentDate
            }, a => a.Id == serverId && a.IsOnline == true && a.BindIndex == serverIndex);
        }

        public long FindServiceId(long systemType, string mac, int serverIndex)
        {
            return this._Db.Get(c => c.SystemType == systemType && c.ServerMac == mac && c.ServerIndex == serverIndex, c => c.Id);
        }
        public long FindServiceId(Guid containerId)
        {
            return this._Db.Get(c => c.ContainerId == containerId, c => c.Id);
        }
    }
}
